package com.watson.juc.testlist;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author watson
 * @date 2021/7/30 10:11
 * @Description: TODO
 **/
public class CallableTest {
    public static void main(String[] args) {
//        new Thread(new Runnable()).start();
//        new Thread(new FutureTask<>(Callable)).start();
        MyThread myThread = new MyThread();

        //适配类
        FutureTask futureTask = new FutureTask(myThread);
        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start(); //多个线程的时候，结果会被缓存，只打印一个
        try {
            //该get方法可能会产生阻塞，一般将他放到最后
            //或者使用异步通信来处理
            Object o = futureTask.get();
            System.out.println(o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

class MyThread implements Callable<Object> {

    @Override
    public Object call() throws Exception {
        System.out.println("fdsfds");
        return "null";
    }
}
